package com.suguowen.sblog.mapper;

import com.suguowen.sblog.bean.Tag;
import com.suguowen.sblog.bean.vo.ArticledetailVO;
import com.suguowen.sblog.bean.vo.ArticlesVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface DIYMapper {
    /**
     * a.art_id,
     * title,
     * create_time, 创作时间
     * nickname, 作者名
     * html_content, 展示页面
     * t.name as tag_name 标签
     */
    @Select("select a.art_id,title,create_time,nickname,html_content,t.name as tag_name,a.tag_id,reading_quantity from " +
            "(article a, user_info ui, article_detail ad) left join tag t on a.tag_id = t.id\n" +
            "where a.user_id = ui.user_id and a.art_id = ad.art_id order by create_time desc")
    @Results({
            @Result(column="html_content",property="html_content"),
            @Result(column="reading_quantity",property="readingQuantity"),
    })
    public List<ArticlesVO> selectArticle();

    @Select("select a.art_id,title,create_time,nickname,html_content,t.name as tag_name,a.tag_id,reading_quantity from (article a, user_info ui, article_detail ad) left join tag t on a.tag_id = t.id where a.user_id = ui.user_id and a.art_id = ad.art_id and a.tag_id = #{id} order by create_time desc")
    @Results({
            @Result(column="html_content",property="html_content"),
            @Result(column="reading_quantity",property="readingQuantity"),
    })
    public List<ArticlesVO> selectArticleByTag(Integer id);

    @Select("select a.art_id,title,create_time,nickname,html_content,t.name as tag_name,a.tag_id,reading_quantity from (article a, user_info ui, article_detail ad) left join tag t on a.tag_id = t.id where a.user_id = ui.user_id and a.art_id = ad.art_id and (year(create_time)= #{year} AND month(create_time)= #{month} ) order by create_time desc")
    @Results({
            @Result(column="html_content",property="html_content"),
            @Result(column="reading_quantity",property="readingQuantity"),
    })
    public List<ArticlesVO> selectArticleByDate(@Param("year")Integer year, @Param("month")Integer month);

    @Results({@Result(column="art_id",property="artId"),
            @Result(column="md_content",property="markdown"),
            @Result(column="title",property="title"),
            @Result(column="nickname",property="nickname"),
            @Result(column="create_time",property="createTime"),
            @Result(column="reading_quantity",property="readingQuantity"),
    })
    @Select("select a.art_id,md_content,title,nickname,create_time,reading_quantity,a.user_id from article_detail ad,article a,user_info ui where a.art_id = ad.art_id and a.user_id = ui.user_id and a.art_id = #{id}")
    public ArticledetailVO selectArticledetailByArtId(Integer id);

    @Select("select t.id,t.name\n" +
            "  from\n" +
            "    tag t,article a\n" +
            "where t.id = a.tag_id and a.art_id = #{id}\n")
    public Tag selectTagByArtId(Integer id);

}
